﻿--下发记录
CREATE PROCEDURE [dbo].[proc_InOutSide_MyCustomerList]
(
	@UserId int,
	@StartIndex int,
	@EndIndex INT,
	@StartDate nvarchar(10),
	@EndDate nvarchar(10)
)
AS
BEGIN
	Declare @sRq Datetime,@eRq Datetime
	Set @sRq = Convert(Datetime,@StartDate+' 00:00:00')
	Set @eRq = Convert(Datetime,@EndDate+' 23:59:59')

	;WITH list As (Select ROW_NUMBER() OVER (ORDER BY In_Date Desc) AS Row,
		A.Id,
		C.delFlag,
		C.CustomerId,
		C.CustomerName,
		C.Project,
		C.ProtectId,
		IsNull((Select Title From Customer_Source Where Id=C.SourceId),'...') As Source,
		IsNull(A.Out_Date,'1900-01-01') As ProtectDate,
		IsNull((Select Title From Customer_Industry Where Id=C.IndustryOneId),'...') As IndustryOne,
		IsNull((Select Title From Customer_Industry Where Id=C.IndustryTwoId),'...') As IndustryTwo,
		IsNull((Select Title From Customer_Type Where Id=C.TypeId),'...') As Type,
		IsNull((Select Top 1 EName From Employee Where Employee.UserName=A.InSide),'') As SalesPerson,
		IsNull((Select Top 1 EName From Employee Where Employee.UserName=A.OutSide),'') As OutSidePerson,
		A.If_Date,
		Case When [Out_Date] Is Null Then 0 Else 1 End As Out_DateFlag,
		A.In_Date As TrackDate
	From InOutSide A,Customer C
	Where A.IfSide=@UserId And A.CusId=C.CustomerId And If_Date>=@sRq And If_Date<=@eRq)

	Select *,(Select Count(0) From List) As RecordCount From List Where Row Between @StartIndex And @EndIndex Order By Row
END
